package net.liteheaven.xblib.common.log;

import android.content.Context;

import net.liteheaven.XbApp;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;

import java.io.File;
import java.io.IOException;

/**
 * 日志收集模块
 * Created by liangy on 2016/11/1.
 */
public class Log4jManager {

    private static final int FILE_COUNT = 2;
    private static final long FILE_SIZE = 1024L * 1024L;
    private static Log4jManager sInstance;

    private boolean writeFile = false;
    private boolean writeConsole = false;
    private Level logLevel = Level.OFF;

    public static Log4jManager getInstance(){
        if (sInstance == null){
            sInstance = new Log4jManager();
        }
        return sInstance;
    }

    private Context ctx(){
        return XbApp.getInstance().getContext();
    }

    private Log4jManager(){}

    private void setWriteFile(boolean val){
        writeFile = val;
    }

    private void setWriteConsole(boolean val){
        writeConsole = val;
    }

    private void setLogLevel(Level val){
        logLevel = val;
    }

    private boolean configureImpl(){
        File fDir = Log4jConfig.getLogFolderPath(ctx());
        if (!fDir.exists()){
            return false;
        }

        File fFile = Log4jConfig.getLogFilePath(ctx());

        Logger logger = getLogger();
        logger.setLevel(logLevel);

        logger.removeAllAppenders();

        if (writeConsole){
            try{
                ConsoleAppender ca = null;
                PatternLayout layout = new PatternLayout("%t %c %p - %m%n");
                ca = new ConsoleAppender(layout);
                ca.setTarget(ctx().getPackageName());
                logger.addAppender(ca);
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        if (writeFile){
            try {
                RollingFileAppender fa;
                PatternLayout layout = new PatternLayout("%t %c %p - %m%n");
                fa = new RollingFileAppender(layout, fFile.toString());
                fa.setMaxBackupIndex(FILE_COUNT);
                fa.setMaximumFileSize(FILE_SIZE);
                fa.setImmediateFlush(true);
                logger.addAppender(fa);
            } catch (IOException var8) {
                throw new RuntimeException("Exception configuring log system", var8);
            }
        }
        BasicConfigurator.configure();
        return true;
    }

    public void configure(boolean release){
        if (!release){
            setWriteConsole(true);
            setWriteFile(true);
            setLogLevel(Level.ALL);
        }else{
            setWriteConsole(false);
            setWriteFile(true);
            setLogLevel(Level.INFO);
        }
        configureImpl();
    }

    Logger getLogger(){
        //Logger logger = Logger.getRootLogger();
        Logger logger = Logger.getLogger(Log4jManager.class.getSimpleName());
        logger.setAdditivity(false);
        return logger;
    }
}
